package cn.bob.org.controller;

import cn.bob.basic.anno.PreAuthorize;
import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Department;
import cn.bob.org.domain.Employee;
import cn.bob.org.query.DepartmentQuery;
import cn.bob.org.query.EmployeeQuery;
import cn.bob.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bob
 * @data 2022/9/30-9:17
 */
@RestController  //设置这个类返回结果为json格式 数据
@RequestMapping("/emp")
@PreAuthorize
public class EmployeeController {
    @Autowired
    private IEmployeeService service;
    //查询所有数据
    @GetMapping
    @PreAuthorize(name ="查询所有员工数据",sn = "employee:findAll")
    public List<Employee> findAll(){
        List<Employee> all = service.findAll();
        return all;
    }
    //查询单条数据
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return service.findById(id);
    }
    //根据id删除数据
    @DeleteMapping("/{id}")
    public AjaxResult delById(@PathVariable("id") Long id){
        service.delById(id);
        return AjaxResult.me().success();
    }
    //批量删除数据
    @PatchMapping
    public  AjaxResult batchDel(@RequestBody List<Long> ids){
        service.batchDel(ids);
        return  AjaxResult.me().success();
    }
    //添加与修改
    @PutMapping
    public AjaxResult addorup(@RequestBody Employee employee){
        if (null == employee.getId()){
            service.add(employee);
        }else {
            service.update(employee);
        }
        return AjaxResult.me().success();
    }
    //分页查询
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return service.queryByPage(query);
    }

    //抽屉
    @GetMapping("/drawer/{id}")
    public  List<Employee> drawer(@PathVariable("id") Long id){
        if (null==id){
            return null;
        }
        System.out.println(id);
        return service.deawer(id);
    }
    //查询单条数据
    @GetMapping("/selectByShopId/{shopId}")
    public List<Employee> selectByShopId(@PathVariable("shopId") Long shopId){
        return service.selectByShopId(shopId);
    }
}
